package com.itheima.app.controller;

import com.itheima.app.manager.MovementManager;
import com.itheima.domain.mongo.Movement;
import com.itheima.utils.ConstantUtil;
import com.itheima.vo.MovementVo;
import com.itheima.vo.PageBeanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * @author Guanxing
 * @date 2021/9/22
 */
@RestController
public class MovementController {

    @Autowired
    private MovementManager movementManager;

    //发布动态
    @PostMapping("/movements")
    public void saveMovement(Movement movement, MultipartFile[] imageContent) throws IOException {
        movementManager.saveMovement(movement, imageContent);
    }

    //根据id查询用户的动态 --> 我的动态
    @GetMapping("/movements/all")
    public PageBeanVo findMovementByUserId(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            Long userId
    ) {
        return movementManager.findMovementByUserId(page, pageSize, userId);
    }

    //查询好友动态
    @GetMapping("/movements")
    public PageBeanVo findFriendMovement(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        return movementManager.findFriendMovement(page, pageSize);
    }

    //查询推荐动态
    @GetMapping("/movements/recommend")
    public PageBeanVo findRecommendMovement(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        return movementManager.findRecommendMovement(page, pageSize);
    }

    //点赞
    @GetMapping("/movements/{publishId}/like")
    public Integer commentLike(
            @PathVariable String publishId
    ) {
        return movementManager.commentLike(publishId);
    }

    //取消点赞
    @GetMapping("/movements/{publishId}/dislike")
    public Integer commentDisLike(
            @PathVariable String publishId
    ) {
        return movementManager.commentDisLike(publishId);
    }

    //动态喜欢
    @GetMapping("/movements/{id}/love")
    public Integer saveMovementLove(@PathVariable("id") String movementId) {
        return movementManager.saveMovementLove(movementId);
    }

    //动态取消喜欢
    @GetMapping("/movements/{id}/unlove")
    public Integer deleteMovementLove(@PathVariable("id") String movementId) {
        return movementManager.deleteMovementLove(movementId);
    }

    //单条动态
    @GetMapping("/movements/{movementId}")
    public MovementVo findMovementVo(
            @PathVariable String movementId
    ) {
        return movementManager.findMovementVo(movementId);
    }

    //评论列表
    @GetMapping("/comments")
    public PageBeanVo findFriendMovement(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            String movementId
    ) {
        return movementManager.findCommentList(page, pageSize, movementId);
    }

    //发表评论
    @PostMapping("/comments")
    public Integer saveComment(@RequestBody Map map) {
        //动态id
        String movementId = (String) map.get("movementId");
        //评论内容
        String comment = (String) map.get("comment");

        return movementManager.saveComment(movementId, comment);
    }

    //点赞列表
    @GetMapping("/messages/likes")
    public PageBeanVo messagesLikes(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        int commentType = ConstantUtil.MOVEMENT_LIKE_1;
        return movementManager.messagesByCommentType(page, pageSize, commentType);
    }

    //评论列表
    @GetMapping("/messages/comments")
    public PageBeanVo messagesComments(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        int commentType = ConstantUtil.MOVEMENT_LIKE_2;
        return movementManager.messagesByCommentType(page, pageSize, commentType);
    }

    //喜欢列表
    @GetMapping("/messages/loves")
    public PageBeanVo messagesLoves(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        int commentType = ConstantUtil.MOVEMENT_LIKE_3;
        return movementManager.messagesByCommentType(page, pageSize, commentType);
    }

    //公告统计
    @GetMapping("/messages/announcements")
    public PageBeanVo messagesAnnouncements(@RequestParam(defaultValue = "1") Integer page,
                                            @RequestParam(defaultValue = "10") Integer pagesize) {
        return movementManager.messagesAnnouncements(page, pagesize);
    }


}
